﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace MethodCaching.Weaving
{
    /// <summary>
    /// A <see cref="ITypeSelector"/> implementation that uses 
    /// a function delegate to determin the target types.
    /// </summary>
    public class LambdaTypeSelector : ITypeSelector
    {
        private readonly Func<TypeDefinition, bool> _selector;

        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaTypeSelector"/> class.
        /// </summary>
        /// <param name="selector">The function delegate used to select the target types.</param>
        public LambdaTypeSelector(Func<TypeDefinition,bool> selector)
        {
            _selector = selector;
        }

        /// <summary>
        /// Returns a list of types from the target <paramref name="moduleDefinition"/>. 
        /// </summary>
        /// <param name="moduleDefinition">The module that contains the target types.</param>
        /// <returns>The list of selected types.</returns>
        public IEnumerable<TypeDefinition> Select(ModuleDefinition moduleDefinition)
        {
            return moduleDefinition.Types.Where(_selector).ToList();
        }
    }
}
